package com.book.bookshop.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.book.bookshop.entity.Book;
import com.book.bookshop.entity.BookVo;
import com.book.bookshop.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @RequestMapping("/index")
    public String index(){return "index";}

    @ResponseBody
    @RequestMapping("/{id}")
    public String getBookById(@PathVariable("id")Integer id){
        Book byId = bookService.getById(id);
        return byId.toString();

    }

    @RequestMapping("/getBookData")
    public String getBookData(Model model,Integer page,Integer category){
        //mybatis-plus分页
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", category);
        IPage<Book> iPage=bookService.page(new Page<>(page,4),queryWrapper);
        model.addAttribute("bookList",iPage.getRecords());
        model.addAttribute("pre",iPage.getCurrent() - 1);
        model.addAttribute("next",iPage.getCurrent() + 1);
        model.addAttribute("cur",iPage.getCurrent());
        model.addAttribute("last",iPage.getPages());
        model.addAttribute("category",category);
        return "bookData";
    }

    @RequestMapping("/bookList")
    public String bookList(String category,Model model){
        model.addAttribute("category",category);
        return "books_list";
    }

    /**
     * pagesize不一样,,,,,就是这一个
     * @param model
     * @param page
     * @param category
     * @return
     */
    @RequestMapping("/getBookListData")
    public String getBookListData(Model model,Integer page,Integer pageSize,Integer category){
        //mybatis-plus分页
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        // queryWrapper.eq("category", category);
        IPage<Book> iPage=bookService.page(new Page<>(page,pageSize),queryWrapper);
//        model.addAttribute("booklist",iPage.getRecords()); 小写的L没看出来
        model.addAttribute("bookList",iPage.getRecords());
        model.addAttribute("pre",iPage.getCurrent() - 1);
        model.addAttribute("next",iPage.getCurrent() + 1);
        model.addAttribute("cur",iPage.getCurrent());
        model.addAttribute("pages",iPage.getPages());
        model.addAttribute("category",category);
        model.addAttribute("pageSize",pageSize);
        return "booksListData";
    }

    /**
     * 图书详情
     */
    @RequestMapping("/detail")
    public String detail(Integer id,Model model){
        Book book = bookService.getById(id);
        model.addAttribute("book",book);
        return "details";
    }

    /**
     * 搜索图书
     * @param model
     * @param name
     * @return
     */
    @RequestMapping("/search")
    public String bookSerach(Model model,String name){
        model.addAttribute("name",name);
        System.out.println(name);
        return "books_list";
    }

    @RequestMapping("/search2")
    public String bookSerachRevert(Model model,Integer page,Integer pageSize,String name){
        List<BookVo> list =  bookService.serach(name);
        System.out.println(list);
        model.addAttribute("bookList",list);
        return "booksListData";
    }

    // http://localhost:8082/book/book/bookListByFenlei?fenlei=2
    @RequestMapping("/bookListByFenlei")
    public String bookListByFenlei(String fenlei,Model model){
        model.addAttribute("fenlei",fenlei);
        model.addAttribute("type","selectByFenLei");
        return "books_list";
    }

    /**
     * 根据fenlei
     * @param model
     * @param page
     * @param fenlei
     * @return
     */
    @RequestMapping("/getBookListDataByFenlei")
    public String getBookListDataByFenlei(Model model,Integer page,Integer pageSize,String fenlei){
        Integer fenlei_id=Integer.parseInt(fenlei);
        //mybatis-plus分页
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fenlei_id", fenlei_id);
        IPage<Book> iPage=bookService.page(new Page<>(page,pageSize),queryWrapper);
//        model.addAttribute("booklist",iPage.getRecords()); 小写的L没看出来
        model.addAttribute("bookList",iPage.getRecords());
        model.addAttribute("pre",iPage.getCurrent() - 1);
        model.addAttribute("next",iPage.getCurrent() + 1);
        model.addAttribute("cur",iPage.getCurrent());
        model.addAttribute("pages",iPage.getPages());
        model.addAttribute("fenlei",fenlei_id);
        model.addAttribute("pageSize",pageSize);
        return "booksListData";
    }
}
